package com.huanqiu.mylib.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.meituan.android.walle.WalleChannelReader;

import java.util.UUID;

public final class Util {
    private static final String TAG = "Util";
    private static final String SPKEY_InstanceId = "APP_SPKEY_InstanceId";

    private static String sInstanceId;

    public static void showToast(Context ctx, String msg) {
        Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
    }

    public static void showError(Context ctx, String msg) {
        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
    }

    public static boolean isEmulator() {
        return Build.MODEL.contains("Android SDK");
    }

    /**
     * 获取存储模式
     *
     * @return 1: legacy mode, 2: scoped storage
     */
    public static int getStorageMode() {
        if (Build.VERSION.SDK_INT < SystemVersion.Android_10) {
            return 1;
        } else {
            return Environment.isExternalStorageLegacy() ? 1 : 2;
        }
    }

    /**
     * 获取应用名称
     */
    public static String getAppName(Context context) {
        if (context == null) return null;
        try {
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo applicationInfo = context.getApplicationInfo();
            return String.valueOf(packageManager.getApplicationLabel(applicationInfo));
        } catch (Throwable e) {
            //
        }
        return null;
    }

    /**
     * 从 AndroidManifest.xml 文件中读取元数据
     */
    public static String getMetaValue(Context context, String metaKey) {
        if (context == null || metaKey == null) {
            return null;
        }

        Bundle metaData = null;
        String metaValue = null;

        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
                    context.getPackageName(), PackageManager.GET_META_DATA);
            metaData = ai.metaData;
            if (metaData != null) {
                metaValue = metaData.getString(metaKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            //
        }
        return metaValue;
    }

    public static String getUmengChannel(Context context) {
        String channel = getMetaValue(context, "UMENG_CHANNEL");
        return channel != null ? channel : "";
    }

    public static String getWalleChannel(Context context) {
        String channel = WalleChannelReader.getChannel(context.getApplicationContext());
        return channel != null ? channel : "";
    }

    /**
     * 获取实例 ID
     */
    public static String getInstanceId(Context context) {
        if (TextUtils.isEmpty(sInstanceId)) {
            String instanceId = (String) SPUtil.get(context, SPKEY_InstanceId, "");
            if ("".equals(instanceId)) {
                instanceId = _genInstanceId();
                SPUtil.put(context, SPKEY_InstanceId, instanceId);
            }
            sInstanceId = instanceId;
        }
        Log.i(TAG, "getInstanceId(): " + sInstanceId);
        return sInstanceId;
    }

    /**
     * 生成实例 ID
     */
    private static String _genInstanceId() {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString().replace("-", "");
        return str.substring(0, 16);
    }

}
